"""
验证 pwtest 包的完整性和导入功能

运行此脚本以验证:
1. 包能否正确导入
2. 所有公共 API 是否可用
3. 版本信息是否正确
"""

import sys
from pathlib import Path

def verify_imports():
    """验证所有导入是否正常"""
    print("=" * 60)
    print("开始验证 pwtest 包...")
    print("=" * 60)
    
    try:
        # 1. 导入主包
        print("\n✓ 测试 1: 导入主包")
        import pwtest
        print(f"  - 包版本: {pwtest.__version__}")
        print(f"  - 作者: {pwtest.__author__}")
        print(f"  - 许可证: {pwtest.__license__}")
        print(f"  - URL: {pwtest.__url__}")
        
        # 2. 验证配置类
        print("\n✓ 测试 2: 验证配置类")
        from pwtest import Config, settings
        print(f"  - Config 类: {Config}")
        print(f"  - settings 实例: {settings}")
        print(f"  - BASE_URL: {settings.BASE_URL}")
        
        # 3. 验证工具类
        print("\n✓ 测试 3: 验证工具类")
        from pwtest import BrowserStateManager
        print(f"  - BrowserStateManager: {BrowserStateManager}")
        
        # 4. 验证环境信息函数
        print("\n✓ 测试 4: 验证环境信息函数")
        from pwtest import (
            get_system_info,
            get_dependency_versions,
            is_ci_environment,
            collect_all_environment_info,
        )
        print(f"  - get_system_info: {get_system_info}")
        print(f"  - get_dependency_versions: {get_dependency_versions}")
        print(f"  - is_ci_environment: {is_ci_environment}")
        print(f"  - collect_all_environment_info: {collect_all_environment_info}")
        
        # 5. 验证子模块
        print("\n✓ 测试 5: 验证子模块")
        from pwtest.config import Config as ConfigBase
        from pwtest.utils import BrowserStateManager as BSM
        print(f"  - pwtest.config.Config: {ConfigBase}")
        print(f"  - pwtest.utils.BrowserStateManager: {BSM}")
        
        # 6. 验证 __all__ 导出
        print("\n✓ 测试 6: 验证 __all__ 导出")
        print(f"  - __all__ 内容: {pwtest.__all__}")
        
        # 7. 测试配置继承
        print("\n✓ 测试 7: 测试配置继承")
        class TestConfig(Config):
            BASE_URL = "https://test.example.com"
            HEADLESS = True
        
        test_settings = TestConfig()
        print(f"  - 自定义 BASE_URL: {test_settings.BASE_URL}")
        print(f"  - 自定义 HEADLESS: {test_settings.HEADLESS}")
        print(f"  - 继承 VIEWPORT_WIDTH: {test_settings.VIEWPORT_WIDTH}")
        
        print("\n" + "=" * 60)
        print("✅ 所有验证测试通过!")
        print("=" * 60)
        return True
        
    except Exception as e:
        print("\n" + "=" * 60)
        print(f"❌ 验证失败: {e}")
        print("=" * 60)
        import traceback
        traceback.print_exc()
        return False

def check_dependencies():
    """检查依赖是否安装"""
    print("\n检查依赖包...")
    dependencies = [
        "playwright",
        "pytest",
        "loguru",
        "allure",
    ]
    
    missing = []
    for dep in dependencies:
        try:
            __import__(dep)
            print(f"  ✓ {dep}")
        except ImportError:
            print(f"  ✗ {dep} (未安装)")
            missing.append(dep)
    
    if missing:
        print(f"\n⚠️  缺少依赖: {', '.join(missing)}")
        print("请运行: pip install -e .")
        return False
    return True

if __name__ == "__main__":
    print("pwtest 包验证工具\n")
    
    # 检查依赖
    deps_ok = check_dependencies()
    
    # 验证导入
    if deps_ok:
        success = verify_imports()
        sys.exit(0 if success else 1)
    else:
        sys.exit(1)
